/**
 * 工具类
 **/

export const deepCopy = data => {
    // 目前支持对象，数组可能有问题
    return data ? JSON.parse(JSON.stringify(data)) : {}
}

export const setPage = (page, newPage) => {
    if (newPage) {
        const { size, number, pages, total, filter } = newPage
        if (pages >= 0) page.pages = pages
        if (total >= 0) page.total = total
        if (size >= 0) page.size = size
        if (number >= 1) page.number = number - 1
        if (filter) page.filter = { ...page.filter, ...filter }
    }
}

/**
 * 参数拼接
 */
export const handleParams = objc => {
    if (typeof objc !== 'object') return objc
    let paramsStr = ''
    for (let key in objc) {
        if (!paramsStr) {
            paramsStr = `${key}=${objc[key]}`
        } else {
            paramsStr = `${paramsStr}&${key}=${objc[key]}`
        }
    }
    return paramsStr
}

export const isNumeric = input => {
    var RE = /^-{0,1}\d*\.{0,1}\d+$/
    return RE.test(input)
}

// 将数字保留两位小数
export const keepTwoDecimalFull = num => {
    var newNumber = parseFloat(num)
    if (isNaN(newNumber)) {
        return num
    }
    newNumber = Math.round(num * 100) / 100
    var newString = newNumber.toString()
    var pos_decimal = newString.indexOf('.')
    if (pos_decimal < 0) {
        pos_decimal = newString.length
        newString += '.'
    }
    while (newString.length <= pos_decimal + 2) {
        newString += '0'
    }
    return newString
}
